package com.tmpt.controller.springmvc.handlers;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.tmpt.utils.fastdfs.FastDFSUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Random;

/**
 * Created by wxy on 2017/12/7.
 */
@Controller
@RequestMapping("/springmvc/fdfs")
public class FdfsAction {
    private Logger log = Logger.getLogger(this.getClass());

    private String httpUri = "";

    @RequestMapping(value = "/uploadImages")
    @ResponseBody
    public Map<String,Object> uploadImages(HttpServletRequest request){
        Map<String,Object> result = Maps.newConcurrentMap();
        DefaultMultipartHttpServletRequest multipartHttpServletRequest = (DefaultMultipartHttpServletRequest) request;
        MultipartFile files = multipartHttpServletRequest.getFile("file");
        try {
            log.info("fileName-->" + files.getOriginalFilename() + "     file-size--->" + files.getSize());
            Map<String, Object> retMap = FastDFSUtil.upload(files);
            String code = (String) retMap.get("code");
            String fileId = (String)retMap.get("fileId");
            log.debug("code:" + code);
            log.debug("fileId:" + fileId);
            if ("0000".equals(code)) {
                log.info("文件： " + files.getOriginalFilename() + " 上传成功 ");
                log.info(files.getOriginalFilename() + "返回下载地址：" + httpUri + fileId);
                //保存数据
                result.put("imgUrl", fileId );
            }else{
                result.put("errorCode","23333344");
                result.put("errorMsg","上传文件失败");
                return result;
            }
            result.put("errorCode","000000");
            result.put("errorMsg", "上传文件成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("errorCode","233333");
            result.put("errorMsg","上传文件失败");
        }
        return result;
    }


    @RequestMapping(value = "/uploadImages4Base64")
    @ResponseBody
    public Map<String,Object> uploadImages4Base64(String imgStr){
        Map<String,Object> result = Maps.newConcurrentMap();
        BASE64Decoder decoder = new BASE64Decoder();
        if(imgStr==null || "".equals(imgStr)){
            result.put("errorCode","233333");
            result.put("errorMsg","文件为空，上传失败");
            return result;
        }
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
            String file = new Random().nextInt(99999999)+".png";
            Map<String, Object> retMap = FastDFSUtil.upload(b,file,"png");
            String code = (String) retMap.get("code");
            String fileId = (String)retMap.get("fileId");
            log.debug("code:" + code);
            log.debug("fileId:" + fileId);
            if ("0000".equals(code)) {
                log.info("文件： " + " 上传成功 ");
                log.info("返回下载地址：" + httpUri + fileId);
                //保存数据
                result.put("imgUrl", fileId );
            }else{
                result.put("errorCode","23333344");
                result.put("errorMsg","上传文件失败");
                return result;
            }
            result.put("errorCode","000000");
            result.put("errorMsg", "上传文件成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("errorCode","233333");
            result.put("errorMsg","上传文件失败");
        }
        return result;
    }


    public static void main(String[] args) {
        String imgStr = "JJyBzzGX65Js010jGveejBB3rki3VNuKgY3Mv0e89oAyNg4ngSuilwJu8JGhul7NFzfPn4/EKluGVDJesxp4ttfJRZtpmXfctDWgGw7v1+uatbE8uBsCbak8yV0LMGD5Hve0scTcm+p/X4JuwhpD8gGtFiRnl3f3RWmgER3Gh+trG/f/lbaGZ0e90RNwSNLZKTsCHXLmkAkgbpOuuXdmhmHPhaA5xAtbeJ+CjS3FPYv++75lbLZ/wBgfu/1IQg2tJ+8Sf8AK5bV37vH9w/zIQgwantt+9+S17vZj7zvmUIRK0+KewP3h8wraLR33R//ACQhUZM2rPFv85WBX+0i+5/U5CERyWJ9ryHycsyl9hF4H+lCEjNbeHtx+LvmVQ/20Xg35NQhUjW1nt6jw/pasJvbn+6P5kIRFOF/vcngf5lsTqfv/wBJQhFWzfur/vBYuH+xHjJ/K5CERnV376fuj5FaOp7b/H8UIQc8P/JD/l/qcsiv/e2f9v5yhCxHSrqT2nmPkt9B2Gf8n4tQhbc2zb2p/ut/mcp1XsHfed80IVZUt9oPL+ZWN/8AIzeI+QQhBjVHYZ4n5MU6n2U3/F+IQhFhDtTfe/FqyaD2uJ+fyKEJ8VVU/vD/ABK1EvZPgfwQhZabDA+1H4/kunZ+4T/8Z/lchCQZ8fs4/wDiP4LFn/2PvM/mQhRYdR2h98fJZ7/ZHwPyQhFQZ7H/APCfxQ72bPuD8UIRX//Z";
        imgStr = "YMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwICBx8AARRoAAW5jpFUAAAAASUVORK5CYII=";
        System.out.println(imgStr.length());
        Map<String,Object> result = new FdfsAction().uploadImages4Base64(imgStr);
        //{"errorCode":"000000","errorMsg":"上传文件成功","imgUrl":"group1/M00/00/17/wKhuZF4OsfuAJeYPAAACKKUN5D8640.png"}
        System.out.println(new Gson().toJson(result));
    }

}
